package com.emp.dao;

import java.util.Set;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.emp.bean.User;

@Repository//��ʶ����һ����ݷ��ʲ����
public interface UserDao {	 

	@Select(" select * from t_user"
			+ " where user_name=#{username} "
			+ " and user_pass=#{password} ")
	@Results({
		 @Result(id=true,column="user_id",property="userId"),
		 @Result(column="user_name",property="username"),
		 @Result(column="user_pass",property="password")		
	})

	User queryUser(@Param("username")String username,
			@Param("password") String password) throws Exception;
	

	@Select(" select count(*) from t_user where user_name=#{username}")
	@ResultType(int.class)
	int queryUserByName(@Param("username")String username) throws Exception;

	@Insert(" insert into t_user values (#{userId},#{username},#{password},#{salt}) ")
	void save(User user) throws Exception;

    //依据用户名查询用户
	@Select(" select * from t_user where user_name=#{username}")
	@Results({
			@Result(id=true,column="user_id",property="userId"),
			 @Result(column="user_name",property="username"),
			 @Result(column="user_pass",property="password"),
			 @Result(column="user_sal",property="salt")
	})
	User queryUserByName2(String username) throws Exception;

    
	//依据用户名查询用户对应的所有角色
	@Select(" select  r.role_name"
			+ " from t_user u inner join t_user_role ur "
			+ " on u.user_id = ur.user_id "
			+ " inner join t_role r "
			+ " on ur.role_id = r.role_id "
			+ " where u.user_name = #{userName}" )
	@ResultType(String.class)
	Set<String> queryRoles(@Param("userName")String userName);


	//依据用户名查询该用户所有的权限
	@Select(" select p.per_name"
			+ "  from t_user u inner join t_user_role ur "
			+ " on u.user_id = ur.user_id "
			+ " inner join t_role r "
			+ " on ur.role_id = r.role_id "
			+ " inner join t_role_permission rp"
			+ " on r.role_id = rp.role_id "
			+ " inner join t_permission p "
			+ "on rp.per_id = p.per_id "
			+ " where u.user_name = #{userName}")
	@ResultType(String.class)
	Set<String> queryPermission(@Param("userName")String userName);
}
